<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->
    
        <meta name="description" content="同源&amp;amp;跨域
同源同源策略是浏览器的一种安全策略，所谓同源是指，域名，协议，端口完全相同。同一个顶级域名下可以操作
跨域不同源则跨域具有src属性的标签具有跨域能力link iframe  script等
跨域方案1、顶级域名相同的可以通过domain.name来解决，即同时设置 domain">
    

    <!--Author-->
    
        <meta name="author" content="John Doe">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="同源&amp;跨域"/>
    

    <!--Open Graph Description-->
    

    <!--Open Graph Site Name-->
    <meta property="og:site_name" content="Hexo"/>

    <!--Type page-->
    
        <meta property="og:type" content="article" />
    

    <!--Page Cover-->
    

        <meta name="twitter:card" content="summary" />
    

    <!-- Title -->
    
    <title>同源&amp;跨域 - Hexo</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet" />

    <!-- Google Analytics -->
    


    <!-- favicon -->
    
	
</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">逐风</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>同源&跨域</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                        
                            2017-03-08
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>同源&amp;跨域</p>
<h4 id="同源"><a href="#同源" class="headerlink" title="同源"></a>同源</h4><p>同源策略是浏览器的一种安全策略，所谓同源是指，域名，协议，端口完全相同。<br>同一个顶级域名下可以操作</p>
<h4 id="跨域"><a href="#跨域" class="headerlink" title="跨域"></a>跨域</h4><p>不同源则跨域<br>具有src属性的标签具有跨域能力link iframe  script等</p>
<h4 id="跨域方案"><a href="#跨域方案" class="headerlink" title="跨域方案"></a>跨域方案</h4><p>1、顶级域名相同的可以通过domain.name来解决，即同时设置 domain.name = 顶级域名<br>解决数据传递<br>2、window.name + iframe<br>3、location.hash + iframe<br>4、window.postMessage()</p>
<p>参考资料 <a href="http://rickgray.me/2015/09/03/solutions-to-cross-domain-in-browser.html" target="_blank" rel="external">http://rickgray.me/2015/09/03/solutions-to-cross-domain-in-browser.html</a></p>
<h4 id="jsonp解决跨域-主"><a href="#jsonp解决跨域-主" class="headerlink" title="jsonp解决跨域(主)"></a>jsonp解决跨域(主)</h4><p>通过script标签去请求，都是get请求，并把后台数据解析成js并执行<br>其本质是利用了标签具有可跨域的特性，(加上一个async变成异步)前端提供一个回调函数， 由服务端返回此函数的调用，<br>并且将服务器数据以该函数参数的形式传递过来， 此方法需要前后端配合完成。<br>比较方便的方法是动态创建script标签，函数名问题，把函数名当做参数传递给后端，后端接收<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">var script = document.createElement(&apos;script&apos;);</div><div class="line">  // var param = &apos;?flag=123&amp;callback=hello&apos;;</div><div class="line">  // script.src = &apos;http://doc.studyit.com/data.php&apos; + param;</div><div class="line">  // var head = document.getElementsByTagName(&apos;head&apos;)[0];</div><div class="line">  // head.appendChild(script);</div><div class="line">  </div><div class="line">    $cbName = $_GET[&apos;abc&apos;];// callback名字后台一般是写死的</div><div class="line"></div><div class="line">	</div><div class="line">	jQuery  ajax中</div><div class="line">  //   jsonp : &apos;abc&apos;,// 该属性的作用就是定制回调函数的key（等号左边的部分）</div><div class="line">  //   jsonpCallback : &apos;helloworld&apos;,// 该属性的作用就是定制回调函数的value（等号后面的部分）</div></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;script&gt;</div><div class="line">    function fuc(data)&#123;</div><div class="line">        console.log(data.name);</div><div class="line">    &#125;</div><div class="line">&lt;/script&gt;</div><div class="line">&lt;script async src=&quot;http://www.aaaa.com/api.php?callback=fuc&quot;&gt;&lt;/script&gt;</div></pre></td></tr></table></figure>
<p>jsonp解决跨域都是get方式，承载信息量有限，所有数据较大时可以考虑CORS。cors在移动端支持度不错，但是PC 端有兼容问题<br>header(‘Access-Control-Allow-Origin:允许的地址[/*表示允许所有]’)</p>
<p>同源：协议，域名，端口号等相同<br>ajax不允许跨域得到数据<br>解决方案<br>jsonp   Apache反向代理<br>jsonp原理 利于有src属性的标签的跨域能力</p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr />

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2018 John Doe<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>